home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl1 / examples / text / text.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  4.7 KB  |  224 lines

  1. /*
  2.  * Copyright 1993, 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* text.c - render a bitmap strings using X Window System 
  19.  *      fonts and a stroke font strings 
  20.  *
  21.  *    Escape key     - exit the program
  22.  */
  23.  
  24. #include <GL/gl.h>
  25. #include <GL/glu.h>
  26. #include <GL/glut.h>
  27.  
  28. #include <stdio.h>
  29.  
  30. /*  Function Prototypes  */
  31.  
  32. GLvoid  initgfx( GLvoid );
  33. GLvoid  drawScene( GLvoid );
  34. GLvoid  animate( GLvoid );
  35. GLvoid  visibility( int );
  36. GLvoid  reshape( GLsizei, GLsizei );
  37. GLvoid  keyboard( GLubyte, GLint, GLint );
  38.  
  39. void printHelp( char * );
  40.  
  41. /* Global Definitions */
  42.  
  43. #define KEY_ESC    27    /* ascii value for the escape key */
  44.  
  45. /* Global Variables */
  46.      
  47. static GLdouble    left, right, bottom, top;
  48. static void       *fixedFont, *strokeFont;
  49. static char        *charStr = "A text string.";
  50.  
  51. GLvoid
  52. main( int argc, char *argv[] )
  53. {
  54.     GLsizei width, height;
  55.  
  56.     glutInit( &argc, argv );
  57.  
  58.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  59.     height = glutGet( GLUT_SCREEN_HEIGHT );
  60.     glutInitWindowPosition( 0, height / 4 );
  61.     glutInitWindowSize( (width / 2) - 4, height / 2 );
  62.     glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
  63.     glutCreateWindow( argv[0] );
  64.  
  65.     initgfx();
  66.  
  67.     glutIdleFunc( animate );
  68.     glutVisibilityFunc( visibility );
  69.     glutKeyboardFunc( keyboard );
  70.     glutReshapeFunc( reshape );
  71.     glutDisplayFunc( drawScene ); 
  72.  
  73.     printHelp( argv[0] );
  74.  
  75.     glutMainLoop();
  76. }
  77.  
  78. void
  79. printHelp( char *progname )
  80. {
  81.     fprintf(stdout, "\n%s - render a bitmap font string and a "
  82.         "stroke font string\n\n"
  83.         "Escape Key        - exit the program\n\n",
  84.         progname);
  85. }
  86.  
  87. GLvoid
  88. initgfx( GLvoid )
  89. {
  90.     GLdouble    aspect;
  91.  
  92.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  93.     glShadeModel( GL_FLAT );
  94.  
  95.     /* Set up two fonts to use for rendering */
  96.     fixedFont = GLUT_BITMAP_TIMES_ROMAN_24;
  97.     strokeFont = GLUT_STROKE_ROMAN;
  98. }
  99.  
  100. GLvoid 
  101. keyboard( GLubyte key, GLint x, GLint y )
  102. {
  103.     switch (key) {
  104.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  105.         exit(0);
  106.     }
  107. }
  108.  
  109. GLvoid
  110. reshape( GLsizei width, GLsizei height )
  111. {
  112.     GLdouble    aspect;
  113.  
  114.     glViewport( 0, 0, width, height );
  115.  
  116.     aspect = (GLdouble) width / (GLdouble) height;
  117.     if ( aspect < 1.0 ) {
  118.         left = -2.0;
  119.         right = 2.0;
  120.         bottom = -2.0 * ( 1.0 / aspect );
  121.         top = 2.0 * ( 1.0 / aspect );
  122.     } else {
  123.         left = -2.0 * aspect;
  124.         right = 2.0 * aspect;
  125.         bottom = -2.0;
  126.         top = 2.0;
  127.     }
  128.  
  129.     glMatrixMode( GL_PROJECTION );
  130.     glLoadIdentity();
  131.     glOrtho( left, right, bottom, top, -1.0, 1.0 );
  132.     glMatrixMode( GL_MODELVIEW );
  133. }
  134.  
  135. GLvoid 
  136. animate( GLvoid )
  137. {
  138.     /* Tell GLUT to redraw the scene */
  139.     glutPostRedisplay();
  140. }
  141.  
  142. GLvoid
  143. visibility( int state ) 
  144. {
  145.     if (state == GLUT_VISIBLE) {
  146.         glutIdleFunc( animate );
  147.     } else {
  148.         glutIdleFunc( NULL );
  149.     }
  150. }
  151.  
  152. GLvoid
  153. renderBitmapString( void *font, char *string )
  154. {
  155.         int i;
  156.         int len = (int) strlen(string);
  157.         for (i = 0; i < len; i++) {
  158.                 glutBitmapCharacter(font, string[i]);
  159.         }
  160. }
  161.  
  162. GLvoid
  163. renderStrokeString( void *font, char *string )
  164. {
  165.         int i;
  166.         int len = (int) strlen(string);
  167.         for (i = 0; i < len; i++) {
  168.                 glutStrokeCharacter(font, string[i]);
  169.         }
  170. }
  171.  
  172.  
  173. GLvoid
  174. drawScene( GLvoid )
  175. {
  176.     char              str[16];
  177.     static GLfloat    whiteColor[] = { 1.0, 1.0, 1.0 };
  178.     static GLfloat    x = 3.0;
  179.  
  180.     glClear( GL_COLOR_BUFFER_BIT );
  181.  
  182.     XYaxes();
  183.  
  184.     /* update the x position each time we're called;
  185.      * wrap when we go off the screen */
  186.     x -= 0.02;
  187.     if ( x <= left )
  188.         x = right ;
  189.  
  190.     glColor3fv( whiteColor );    
  191.  
  192.     glPushMatrix();
  193.         /* Bitmap characters ARE NOT affected by 
  194.          * modeling transformations 
  195.          */
  196.         glRotatef( 15.0, 0.0, 0.0, 1.0 );
  197.         glScalef( 2.0, 2.0, 2.0 );
  198.  
  199.         /* Set position for the left end of the baseline for
  200.          *  our text string */
  201.         glRasterPos2f( x, 0.0 );
  202.         renderBitmapString( fixedFont, charStr );
  203.  
  204.         /* show the baseline */
  205.         glBegin( GL_LINES );
  206.                glVertex2f( x, 0.0 );
  207.                glVertex2f( x+1.0, 0.0 );
  208.         glEnd();
  209.     glPopMatrix();
  210.  
  211.     glPushMatrix(); 
  212.         /* Stroke characters ARE affected by 
  213.          * modeling transformations 
  214.          */
  215.         glTranslatef( -1.0, 0.0, 0.0 );
  216.         glRotatef( 15.0, 0.0, 0.0, 1.0 );
  217.         glScalef(0.003, 0.003, 0.003);
  218.         sprintf( str, "x = %5.2f", x );
  219.         renderStrokeString( strokeFont, str );
  220.     glPopMatrix();
  221.  
  222.     glutSwapBuffers();
  223. }
  224.